require 'net/https'
require 'uri'
require 'openssl/ssl'
Net::HTTP.version_1_2

module Net
  class HTTPS < HTTP
    def initialize(address, port = nil)
      super(address, port)
      self.verify_mode = OpenSSL::SSL::VERIFY_NONE
      self.use_ssl = true
    end
  end
end 
module Infolr
  class GoogleReaderApi
    HOST = 'www.google.com'
    READING_LIST_URL = '/reader/atom/user/-/state/com.google/reading-list'
    GOOGLE_READER_API_STAR_ITEM = 'user/-/state/com.google/starred' 
    GOOGLE_READER_API_SHARE_ITEM = 'user/-/state/com.google/broadcast' 
    GOOGLE_READER_API_READ_ITEM = 'user/-/state/com.google/read' 
    GOOGLE_READER_API_UNREAD_ITEM = 'user/-/state/com.google/kept-unread' 
    # login into google reader service and return related cookie info
    def self.login(email, password)
      service = 'reader'
      continue = 'https://www.google.com/reader'
      nui = '1'
      url = URI.parse('https://www.google.com/accounts/ServiceLoginAuth')
      
      response = Net::HTTPS.post_form(url,
        {'Email' => email, 'Passwd' => password, 'service' => service, 'continue' => continue, 'nui' => nui}
      )
      return response['set-cookie']
    end
    # get the unread items from google reader
    def self.get_reading_list(cookie, count = 100)
      request = Net::HTTP::Get.new(READING_LIST_URL + "?n=#{count}") 
      request['cookie'] = cookie
      response = Net::HTTP.start(HOST) { |http|
        http.request(request)
      }
      return response.body
    end
    def self.get_token(cookie)
      request = Net::HTTP::Get.new('/reader/api/0/token')
      request['cookie'] = cookie
      response = Net::HTTP.start('www.google.com'){ |http|
        http.request(request)
      }
      return response.body 
    end
    # star a feed item
    def self.star_item(cookie,token,feed_entry)
      self.tag_item(cookie,token,feed_entry,:star)
    end
    # mark a feed item as read
    def self.read_item(cookie,token,feed_entry)
      self.tag_item(cookie,token,feed_entry,:read)
    end
    def self.unread_item(cookie,token,feed_entry)
      self.tag_item(cookie,token,feed_entry,:unread)
    end
    def self.share_item(cookie,token,feed_entry)
      self.tag_item(cookie,token,feed_entry,:share)
    end
    private
    def self.tag_item(cookie,token,feed_entry,action=:read)
      start_url = '/reader/api/0/edit-tag'
      url = URI.parse(start_url)
      request = Net::HTTP::Post.new(start_url) 
      request['cookie'] = cookie
      a = GOOGLE_READER_API_STAR_ITEM
      case action
        when :read
          a = GOOGLE_READER_API_READ_ITEM
        when :unread
          a = GOOGLE_READER_API_UNREAD_ITEM
        when :star
          a = GOOGLE_READER_API_STAR_ITEM
        when :share
          a = GOOGLE_READER_API_SHARE_ITEM
      end 
      request.set_form_data(
        's' => feed_entry.feed_url,
        'i' => feed_entry.item_id,
        'a' => a,
        'ac' => 'edit-tags',
        'r' => 'user/-/state/com.google/kept-unread',
        'T' => token
      )
      response = Net::HTTP.start('www.google.com') { |http|
        http.request(request)
      }
    end
  end
end
